package test.my.com.myapplicationtest.surfaceview;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Chronometer;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import test.my.com.myapplicationtest.R;

/**
 * Created by ThinkPad on 2016/2/25.
 * http://www.mamicode.com/info-detail-1113731.html 例子
 */
public class VideoSurfaceActivity extends Activity implements View.OnClickListener,SurfaceHolder.Callback {

    public static final String VIDEO_PATH= "/DCIM/Camera/VID_20160123_180725.mp4";

    private FrameLayout mVideo_control_fl;
    private TextView mPlayedTime_tv;
    private TextView mVideo_length_tv;

    private SurfaceView mVideo_sv;
    private SurfaceHolder mSurfaceHolder;
    private ProgressBar mVideo_pb;
    private MediaPlayer mMediaPlayer;

    private ImageView mStop_iv;
    private ImageView mPlay_iv;
    private ImageView mPause_iv;

    private String path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_video_surface_laout);

        path = Environment.getExternalStorageDirectory().getPath();
        initView();
        initEvent();
        initData();
    }

    private void initView() {
        mVideo_control_fl = (FrameLayout) findViewById(R.id.mVideo_control_fl);
        mPlayedTime_tv = (TextView) findViewById(R.id.mPlayedTime_tv);
        mVideo_length_tv = (TextView) findViewById(R.id.mVideo_length_tv);
        mVideo_pb = (ProgressBar) findViewById(R.id.mVideo_pb);
        mStop_iv = (ImageView) findViewById(R.id.mStop_iv);
        mPlay_iv = (ImageView) findViewById(R.id.mPlay_iv);
        mPause_iv = (ImageView) findViewById(R.id.mPause_iv);
        mVideo_sv = (SurfaceView) findViewById(R.id.mVideo_sv);
    }

    private void initEvent() {
        mStop_iv.setOnClickListener(this);
        mPlay_iv.setOnClickListener(this);
        mPause_iv.setOnClickListener(this);
//        Chronometer
    }

    private void initData() {
        mMediaPlayer = new MediaPlayer();
        mSurfaceHolder = mVideo_sv.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setKeepScreenOn(true);
//        mMediaPlayer.setDisplay(mSurfaceHolder);
        play();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mStop_iv:
                stop();
                break;
            case R.id.mPlay_iv:
                play();
                break;
            case R.id.mPause_iv:
                pause();
                break;
        }
    }

    private void play() {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(path + VIDEO_PATH);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
            mVideo_length_tv.setText(formatTime(mMediaPlayer.getDuration()));
//            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                @Override
//                public void onPrepared(MediaPlayer mp) {
//                    mMediaPlayer.start();
//                }
//            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void stop() {
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.stop();
        }
        mMediaPlayer.reset();
    }

    private void pause() {
        if(mMediaPlayer.isPlaying()){
            mMediaPlayer.pause();
        }else{
            mMediaPlayer.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stop();
    }

    /* SurfaceHolder.Callback 所需要实现的方法 */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mMediaPlayer.setDisplay(mSurfaceHolder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    /**
     * 获取时间格式
     * @return
     */
    private String formatTime(long timeDuration) {
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
        return formatter.format(new Date(timeDuration));
    }
}
